package com.carl.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.carl.mall.product.service.AttrAttrgroupRelationService;
import com.carl.mall.product.service.AttrService;
import com.carl.mall.product.service.CategoryService;
import com.carl.mall.product.vo.AttrGroupAttrResponseVO;
import com.carl.mall.product.vo.AttrGroupRelationVO;
import com.carl.mall.product.vo.AttrResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.carl.mall.product.entity.AttrGroupEntity;
import com.carl.mall.product.service.AttrGroupService;
import com.carl.common.utils.PageUtils;
import com.carl.common.utils.R;



/**
 * 属性分组
 *
 * @author carl
 * @email 541020344@qq.com
 * @date 2024-03-10 00:31:47
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;


    /**
     * 新建关联
     * /product/attrgroup/attr/relation
     */
    @PostMapping("/attr/relation")
    public R addAttrAttrGroupRelation(@RequestBody List<AttrGroupRelationVO> relationVOList){
        attrAttrgroupRelationService.addAttrAttrGroupRelation(relationVOList);
        return R.ok();
    }
    /**
     * 根据分类id获得属性组和属性信息
     */
    @RequestMapping ("/{catelogId}/withattr")
    public R getAttrGroupAndAttr(@PathVariable("catelogId") Long catelogId){
        //根据三级分类id查询属性和属性组信息
        List<AttrGroupAttrResponseVO> attrGroupAndAttr = attrGroupService.getAttrGroupAndAttr(catelogId);
        return R.ok().put("data", attrGroupAndAttr);
    }

    /**
     * 列表
     * 分页查询
     * 前端提交请求需要封装对应的分页数据
     * {
     *     page:1,//当前页
     *     limit：10,//每页显示记录数
     *     sidx："id",//排序字段
     *     order："asc/desc"//排序方式
     *     key: "小米"//查询的关键字
     * }
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPageWithTree(params,catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] catelogPath = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        return R.ok();
    }

   // /app/product/attrgroup/7/attr/relation?t=1718108299955
    //查询关联关系
    @RequestMapping("/{attrGroupId}/attr/relation")
    public R listByAttrRelationGroup(@PathVariable("attrGroupId") Long attrGroupId){
        List<AttrResponseVO> responseVOS =  attrService.queryByAttrGroupId(attrGroupId);
        return R.ok().put("data",responseVOS);
    }

    ///product/attrgroup/attr/relation/delete 接触关联关系
    @PostMapping("/attr/relation/delete")
    public R removeByEntity(@RequestBody AttrGroupRelationVO[] vos){
        attrService.deleteRelation(vos);
        return  R.ok();
    }

    ///app/product/attrgroup/7/noattr/relation? 查询未关联属性
    @RequestMapping("/{attrGroupId}/noattr/relation")
    public R listNoAttr(@RequestParam Map<String, Object> params,
                         @PathVariable("attrGroupId") Long attrGroupId){
        PageUtils page = attrService.queryPageWithTreeNoAttr(params,attrGroupId);
        return R.ok().put("page", page);
    }




}
